package com.frank.eduservice.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.commonutil.R;
import com.frank.eduservice.entity.EduTeacher;
import com.frank.eduservice.service.EduTeacherService;
import com.frank.eduservice.vo.EduTeacherAddVo;
import com.frank.eduservice.vo.EduTeacherQueryVo;
import com.frank.eduservice.vo.EduTeacherUpdateVo;
import com.frank.servicebase.exception.MyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Frank
 * @since 2021-11-12
 */
@Api("讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("/findAll")
    public R<List<EduTeacher>> list() {
        return R.ok(eduTeacherService.list(null));
    }

    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        boolean res = eduTeacherService.removeById(id);
        if (res) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("分页查询讲师")
    @GetMapping("/page/{current}/{size}")
    public R pageList(@ApiParam(name = "current", value = "当前页码", required = true)
                      @PathVariable("current") Integer current,
                      @ApiParam(name = "size", value = "每页条数", required = true)
                      @PathVariable("size") Integer size) {
        // 创建Page对象
        Page<EduTeacher> page = new Page<>(current, size);
        // 调用方法实现分页
        // 调用方法时，底层封装，把分页的所有数据封装到page对象中
        eduTeacherService.page(page, null);
        return R.ok(page);
    }

    @ApiOperation("多条件查询讲师")
    @PostMapping("/condition/{current}/{size}")
    public R pageList(@PathVariable("current") Integer current,
                      @PathVariable("size") Integer size,
                      @RequestBody(required = false) EduTeacherQueryVo teacherQueryVo) {
        // 创建Page对象
        Page<EduTeacher> page = new Page<>(current, size);
        eduTeacherService.conditionPage(page, teacherQueryVo);
        return R.ok(page);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/add")
    public R<EduTeacher> add(@RequestBody EduTeacherAddVo eduTeacherAddVo) {
        EduTeacher eduTeacher = new EduTeacher();
        BeanUtil.copyProperties(eduTeacherAddVo, eduTeacher);
        boolean res = eduTeacherService.save(eduTeacher);
        return res ? R.ok(eduTeacher) : R.error();
    }

    @ApiOperation("通过id查询讲师")
    @PostMapping("/{id}")
    public R<EduTeacher> getById(@PathVariable("id") String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            throw new MyException("除数不能为0");
        }
        return R.ok(eduTeacher);
    }

    @ApiOperation("修改讲师")
    @PostMapping("/update")
    public R update(@RequestBody EduTeacherUpdateVo eduTeacherUpdateVo) {
        EduTeacher eduTeacher = new EduTeacher();
        BeanUtil.copyProperties(eduTeacherUpdateVo,eduTeacher);
        boolean res = eduTeacherService.updateById(eduTeacher);
        return res ? R.ok() : R.error();
    }

}

